package com.renbang.action.devoloper;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.renbang.model.common.AjaxResult;
import com.renbang.model.sys.SysPower;
import com.renbang.service.sys.SysPowerServiceImp;
@Controller
@RequestMapping(value = "developer/power")
public class SysPowerAction {
	@Autowired
	SysPowerServiceImp sysPowerServiceImp;
	
	//功能分配
	@RequestMapping(value = "/addPower")
	public @ResponseBody AjaxResult addPower(Integer userid,String fnids) {
		SysPower sysPower = new SysPower();
		sysPower.setUserId(userid);
		//先看看之前已有的功能,看现在有没有取消部分以前有的功能
		List<SysPower> powerlist =sysPowerServiceImp.findByModel(sysPower);
		for (SysPower power : powerlist) {
		//如果不包含，那就得去掉它的这个功能
		if(!fnids.contains(String.valueOf(power.getFunId()))){
			sysPowerServiceImp.removeById(power.getPowerId());
		}
		}
		String fnidarr[] = fnids.split(",");
		
		for (String id : fnidarr) {
			sysPower.setFunId(Integer.valueOf(id));
			sysPower.setUserId(userid);
			//看用户存不存在此功能
			List<SysPower> powerList =sysPowerServiceImp.findByModel(sysPower);
			//如果是空就说明是分配了新功能要新增
			if(powerList.size()==0){
				sysPowerServiceImp.add(sysPower);
			}
		}
		return new AjaxResult(AjaxResult.STATUS_OK, null, "功能分配成功");
	}
	
}
